package com.example.nep.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.nep.base.BaseController;
import com.example.nep.base.R;
import com.example.nep.dto.SupervisorDTO;
import com.example.nep.model.Supervisor;
import com.example.nep.service.ISupervisorService;
import com.example.nep.util.JwtUtil;
import com.example.nep.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author han
 * @since 2024-04-09
 */
@CrossOrigin
@RestController
@RequestMapping("supervisor")
public class SupervisorController extends BaseController {
    @Resource
    private ISupervisorService service;
    @PostMapping("register")
    public int register(@RequestBody SupervisorDTO supervisorDTO){
        Supervisor sup = service.getById(supervisorDTO.getTelId());
        if (sup!=null){
            return -1;
        }
        Supervisor supervisor = new Supervisor();
        BeanUtils.copyProperties(supervisorDTO,supervisor);
        boolean save = service.save(supervisor);
        if (save){
            return 1;
        }else {
            return 0;
        }
    }
    @GetMapping("login")
    public R login(@RequestParam String telId,@RequestParam String password){
        Supervisor supervisor = service.getById(telId);
        if (supervisor==null){
            return R.error();
        }
        if (!supervisor.getPassword().equals(password)){
            return R.error();
        }
        UserVO userVO=new UserVO();
        userVO.setTelId(supervisor.getTelId());
        userVO.setRole(0);
        String token = JwtUtil.newToken(userVO);
        supervisor.setPassword("");
        return R.ok().setResult("user",supervisor).setResult("token",token);
    }
}
